<?php

/*
 * Module Form sender.
 * For sending email messages on specified email address
 * @author Andrey Muhin
 * @version 2.0 
 */

class FormSender extends A_Content  {
	
	/**
	 * Gets FormSender content by id
	 *
	 * @param Integer $id
	 * @return SingleFormSender
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleFormSender($this->_top,$this,$id);
		}
		
		return $this->_cashe_obj[$id];
	}
	
	/**
	 * Adds new FormSender content and returns pointer to it
	 *
	 * @return SingleFormSender
	 */
	public function add()
	{
		$this->_top->_sql->exeDummy("INSERT INTO ".TAB_PREF."form_sender(add_date) VALUES(NOW());");
		$id = $this->_top->_sql->last_insert_id();
		return $this->getById($id);
	}
	
}

class SingleFormSender extends A_SingleContent {

	private $_cashe_obj = array();
	
	public function loader()
	{
		$get_data = $this->_top->_sql->exe("SELECT * FROM ".TAB_PREF."form_sender c WHERE  form_sender_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("FormSender object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
		}
		return true;
	}
	
	public function __get($param)
	{
		if (!isset($this->_data[$param])) {
			throw $this->_top->Utils->PushError("Trying to access to undefined FormSender property: $param", 'Системная ошибка. Обратитесь к разработчикам');
		} else {
			return $this->_data[$param];
		}
	}
	
	public function __set($param,$value)
	{
		if ($param=='form_sender_id' || !isset($this->_data[$param])) {
			throw $this->_top->Utils->PushError("Trying to access to undefined or forbidden FormSender property: $param", 'Системная ошибка. Обратитесь к разработчикам');
		} else {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE ".TAB_PREF."form_sender SET `$param`='$ins_value' WHERE form_sender_id={$this->_content_id}");
			return true;
		}
	}
	
	public function deleter()
	{
		$this->_top->_sql->exeDummy("DELETE FROM ".TAB_PREF."form_sender WHERE form_sender_id={$this->_content_id}");
	}
	
	/**
	 * Adds new SingleFormSenderMessage content and returns pointer to it.
	 * Actualy it is adding a new user message in journal
	 *
	 * @return SingleFormSenderMessage
	 */
	public function add()
	{
		$this->_top->_sql->exeDummy("INSERT INTO ".TAB_PREF."form_sender_messages(form_sender_id,datetime) VALUES({$this->_content_id},NOW());");
		$id = $this->_top->_sql->last_insert_id();
		return $this->getById($id);
	}
	
	/**
	 *
	 * @return SingleFormSenderMessage
	 */
	public function getById($id) {
		if (!isset($this->_cashe_obj[$id])) {
			$this->_cashe_obj[$id] = new SingleFormSenderMessage($this->_top,$this,$id);
		}
		
		return $this->_cashe_obj[$id];
	}
	
	
	/**
	 * Returns pointer to SingleFormSenderMessageList
	 *
	 * @return SingleFormSenderMessageList
	 */
	public function getList() {
		return new SingleFormSenderMessageList($this->_top,$this,$this->_content_id);
	}
	
	
}

class SingleFormSenderMessageList extends A_SingleContent 
{
	private $list = array();

	public function loader() {
		$get_data = $this->_top->_sql->exe("SELECT `message_id` FROM `cms_form_sender_messages` WHERE `form_sender_id`={$this->_content_id}");
		if (count($get_data)>0) {
			for ($d=0;$d<count($get_data);$d++) {
				array_push($this->list,$get_data[$d]['message_id']);
			}
		}
		
		return true;
	}
	
	public function __set($param,$value) {
		throw $this->_top->Utils->PushError("Trying to access to undefined or forbidden SingleFormSenderMessageList property: $param", 'Системная ошибка. Обратитесь к разработчикам');
	}
	
	public function __get($param) {
		switch (strtolower($param)) {
			case 'item':
				return $this->list;
				break;
				
			case 'length':
				return $this->length();
				break;
		}
		
		throw $this->_top->Utils->PushError("Trying to access to undefined or forbidden SingleFormSenderMessageList property: $param", 'Системная ошибка. Обратитесь к разработчикам');
		
	}
	
	public function length() {
		return count($this->list);
	}
	
	/**
	 * Returns point to specific SingleFormSenderMessage Object
	 *
	 * @param Integer $idx
	 * @return SingleFormSenderMessage
	 */
	public function item($idx) {
		if (!isset($this->list[$idx])) {
			throw $this->_top->Utils->PushError("Trying to access to undefined list item", 'Системная ошибка. Обратитесь к разработчикам');
		}
		return $this->_parent->getById($this->list[$idx]);
	}
	
	public function deleter() {
		throw $this->_top->Utils->PushError("Method not set", 'Системная ошибка. Обратитесь к разработчикам');
	}
	
}

class SingleFormSenderMessage extends A_SingleContent {
	
	public function loader()
	{
		$get_data = $this->_top->_sql->exe("SELECT * FROM ".TAB_PREF."form_sender_messages c WHERE  message_id={$this->_content_id}");
		if (count($get_data)==0)
			throw $this->_top->Utils->PushError("FormSenderMessage object Id: {$this->_content_id} not found", 'Системная ошибка. Обратитесь к разработчикам');
		else {
			$this->_data = $get_data[0];
		}
		return true;
	}
	
	public function __get($param)
	{
		if (!isset($this->_data[$param])) {
			throw $this->_top->Utils->PushError("Trying to access to undefined FormSenderMessage property: $param", 'Системная ошибка. Обратитесь к разработчикам');
		} else {
			return $this->_data[$param];
		}
	}
	
	
	public function __set($param,$value)
	{
		$param = strtolower($param);
		if ($param=='form_sender_id' || $param=='datetime' || $param=='message_id' || $param== !isset($this->_data[$param])) {
			throw $this->_top->Utils->PushError("Trying to access to undefined or forbidden FormSenderMessage property: $param", 'Системная ошибка. Обратитесь к разработчикам');
		} else {
			$this->_data[$param] = $value;
			$ins_value = $this->_top->Utils->prepareValueForSQL($value);
			$this->_top->_sql->exeDummy("UPDATE ".TAB_PREF."form_sender_messages SET `$param`='$ins_value' WHERE message_id={$this->_content_id}");
			return true;
		}
	}
	
	public function makeReaded() {
		$this->read_f = 1;
	}
	
	
	public function deleter()
	{
		$this->_top->_sql->exeDummy("DELETE FROM ".TAB_PREF."form_sender_messages WHERE message_id={$this->_content_id}");
	}
	
}
